<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_order_uqcms extends control
{
    function __construct()
    {
        $this->spid = $_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function send_goods()
    {
        if ($_POST) {
            $oid = P('oid', '订单号提交错误');
            $row = $this->db->get_one("select id,shop_id,order_no,order_status from " . table('order_info') . " where shop_id = " . $this->spid . " and order_no = '" . $oid . "'");
            if ($row && $row['id']) {
                if ($row['order_status'] == 2 || $row['order_status'] == 3) {
                    $this->db->autocommit();
                    $order_query = $this->db->update(table('order_info'), array('order_status' => '3', 'deliver_time' => time()), 'id = ' . $row['id']);
                    $ogs_query = $this->db->update(table('order_goods'), array('order_status' => '3'), 'oid = ' . $row['id']);
                    $kd_code = P('kd_code', '请选择快递');
                    $kd_number = $this->module('express')->apply_code($kd_code);
                    if (!$kd_number) {
                        $kd_number = P('kd_number', '快递单号不能为空');
                    }
                    $kd_arr['kd_code'] = $kd_code;
                    $kd_arr['kd_number'] = $kd_number;
                    $kd_arr['kd_mktime'] = time();
                    $expr_query = $this->db->update(table('order_address'), $kd_arr, 'oid = ' . $row['id']);
                    if ($order_query && $ogs_query && $expr_query) {
                        $this->db->commit();
                        right_json();
                    } else {
                        $this->db->rollback();
                        error_json('发货失败');
                    }
                } else {
                    error_json('订单还未支付，不能发货。');
                }
            } else {
                error_json('获取订单失败');
            }
        }
    }
    public function del_order()
    {
        $oid = isset($_GET['oid']) ? $_GET['oid'] : exit('ID ERROR');
        $row = $this->db->get_one("select id,order_no,shop_id,order_status,del_user,del_shop from " . table('order_info') . " where shop_id = " . $this->spid . " and order_no = " . $oid);
        if ($row['id'] && $row['order_status'] >= 4) {
            if ($row['del_user'] == '1') {
                $this->db->autocommit();
                $q1 = $this->db->delete(table('order_info'), 'id = ' . $row['id']);
                $q2 = $this->db->delete(table('order_goods'), 'oid = ' . $row['id']);
                $q3 = $this->db->delete(table('order_address'), 'oid = ' . $row['id']);
                $q4 = $this->db->delete(table('order_score'), 'oid = ' . $row['id']);
                if ($q1 && $q2 && $q3 && $q4) {
                    $this->db->commit();
                    showmsg('删除成功');
                } else {
                    $this->db->rollback();
                    showmsg('删除失败');
                }
            } else {
                $query = $this->db->update(table('order_info'), array('del_shop' => '1'), 'id = ' . $row['id']);
                if ($query) {
                    showmsg('删除成功');
                } else {
                    showmsg('删除失败');
                }
            }
        } else {
            showmsg('订单状态无法删除');
        }
    }
    public function remark()
    {
        $oid = isset($_GET['oid']) ? $_GET['oid'] : error_json('编号错误');
        $row = $this->db->get_one("select * from " . table('order_info') . " where order_no = " . $oid);
        if ($row && $row['id']) {
            $this->assign('row', $row);
        }
        $this->display();
    }
    public function remark_save()
    {
        if ($_POST) {
            $setarr['remark'] = P('remark');
            $order_no = P('oid', '订单号不能为空');
            $query = $this->db->update(table('order_info'), $setarr, 'order_no = ' . $order_no);
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function seller_submit()
    {
        if ($_POST) {
            $service_id = P('service_id', '提交售后错误');
            $row = $this->db->get_one("select * from " . table('refund') . " where shop_id = " . $this->spid . " and service_id = '" . $service_id . "'");
            if ($row['id']) {
                $post_action = P('is_return', '请选择退货操作');
                if ($post_action == 'return_goods') {
                    $this->db->autocommit();
                    $address_row = $this->db->get_one("select * from " . table('shop_address') . " where spid = " . $this->spid);
                    if ($address_row['id']) {
                        $arr = array($address_row['prov'], $address_row['city'], $address_row['dist']);
                        $addrow = $this->module('district')->get_address($arr);
                        foreach ($addrow as $val) {
                            $addr[] = $val['name'];
                        }
                        $address_name = implode('', $addr);
                    }
                    $con = '退货地址：' . $address_row['name'] . '，' . $address_row['mobile'] . '，' . @$address_name . '，' . $address_row['address'] . '<br>';
                    $con .= '退货说明：' . $address_row['explain'] . '<br>';
                    $content = P('content');
                    if (!empty($content)) {
                        $con .= '退货备注：' . $content . '<br>';
                    }
                    $logArr['rid'] = $row['id'];
                    $logArr['title'] = '卖家：同意退货';
                    $logArr['content'] = $con;
                    $log_query = $this->module('refund')->add_process($logArr);
                    $setsql['status'] = '2';
                    $setsql['passtime'] = time();
                    $query = $this->db->update(table('refund'), $setsql, 'id = ' . $row['id']);
                    if ($log_query && $query) {
                        $this->db->commit();
                        right_json();
                    } else {
                        $this->db->rollback();
                        error_json('操作错误');
                    }
                } elseif ($post_action == 'return_price') {
                    $grow = $this->db->get_one("select id,uid,shop_id,oid,service_id,title,order_status,receipt_time from " . table('order_goods') . " where service_id = '" . $row['service_id'] . "'");
                    if ($grow['order_status'] == '6') {
                        error_json('已经处理完售后');
                    } else {
                        $this->db->autocommit();
                        $user_pay = $this->db->query("update " . table('member') . " set price = price + " . $row['price'] . " where uid = " . $row['uid']);
                        $user_pay_log = $this->module('account')->add($row['uid'], $row['price'], '退款:' . $grow['title'], '1', $row['service_id']);
                        $sb_row = $this->db->get_one("select * from " . table('shop_bill') . " where order_id = " . $grow['oid']);
                        if ($sb_row['id']) {
                            $this->db->query("update " . table('shop_bill') . " set refund_status = 1, refund_time = " . time() . ", price_income = price_income-" . $row['price'] . ", refund_price = refund_price+" . $row['price'] . " where id = " . $sb_row['id']);
                        }
                        $q1 = $this->db->update(table('order_goods'), array('order_status' => '6'), 'id = ' . $grow['id']);
                        $q2 = $this->db->update(table('refund'), array('status' => '5', 'oktime' => time()), 'id = ' . $row['id']);
                        $log_arr['rid'] = $row['id'];
                        $log_arr['title'] = '卖家：同意退款';
                        $log_arr['content'] = '卖家已经同意了退款申请，交易款项' . $row['price'] . '元已归还到你的账户中。';
                        $addlog = $this->module('refund')->add_process($log_arr);
                        $total = $this->db->get_all("select id,oid,order_status from " . table('order_goods') . " where oid = " . $grow['oid']);
                        if ($total) {
                            foreach ($total as $v) {
                                if ($v['order_status'] == '6') {
                                    $th[] = '';
                                }
                            }
                            if (count($total) == count($th)) {
                                $close_query = $this->db->update(table('order_info'), array('order_status' => '6', 'cancel_reason' => '申请退款'), 'id = ' . $grow['id']);
                            }
                        }
                        if ($user_pay && $user_pay_log && $q1 && $q2 && $addlog) {
                            $this->db->commit();
                            right_json();
                        } else {
                            $this->db->rollback();
                            error_json('退款失败');
                        }
                    }
                } elseif ($post_action == 'no_agreen') {
                    $this->db->rollback();
                    $log_arr['rid'] = $row['id'];
                    $log_arr['title'] = '卖家：不同意退货';
                    $log_arr['content'] = '商家不同意退款，平台介入。';
                    $exec = $this->module('refund')->add_process($log_arr);
                    $setsql['status'] = '4';
                    $setsql['admintime'] = time();
                    $query = $this->db->update(table('refund'), $setsql, 'id = ' . $row['id']);
                    if ($query && $exec) {
                        $this->db->commit();
                        right_json();
                    } else {
                        $this->db->rollback();
                        error_json('介入失败');
                    }
                }
            } else {
                error_json('无法获取售后订单');
            }
        } else {
            error_json('操作错误');
        }
    }
    public function seller_order(){
        $shop = $_SESSION['shop']['spid'];
        $user_id = $this->db->get_one("select uid from " . table('shop') . " where id = '{$shop}' limit 1");
        $_SESSION['user']['user_id'] = $user_id['uid'];
        $user_id = $_SESSION['user']['user_id'];
        $sql = "select * from " . table('fbxq') . " as f left join ".table('tb')." as t on t.tb_id = f.id where t.mid = '{$user_id}' order by f.addtime desc";
        $arr =  $this->db->get_all($sql);
        foreach ($arr as $key => $value){
            $goods = unserialize($value['goods_guige']);
            $guige = '';
            foreach ($goods as $g_value){
                foreach ($g_value as $g_k => $g_v){
                    if($g_k == 0){
                        $guige .= '<a class="guige">'.$g_v.'：</a>';
                    }else{
                        $guige .= $g_v;
                    }
                }
            }
            $arr[$key]['goods_guige'] = $guige;
        }
        $this->assign('arr',$arr);
        $this->display();
    }
    public function seller_detail(){
        $id = $_GET['seller_id'];
        if(!empty($id)){
            $sql = "select f.*,u.nick,c.name,t.mjbz,t.money from " . table('fbxq') . " as f left join ".table('member')." as u on f.user_id = u.uid left join ".table('category')." as c on f.goods_fl = c.cid left join ".table('tb')." as t on f.id = t.tb_id where f.id = '{$id}' limit 1";
            $detail = $this->db->get_one($sql);
            $detail['goods_guige'] = unserialize($detail['goods_guige']);
            $this->assign('detail',$detail);
        }else{
            header("Location:member.php?m=goods_order&a=seller_order");
        }
        $this->display();
    }
}